package com.fieldchain.ex.fa100.utils.common;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.view.animation.AnticipateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.custom.view.MyAnimationUtils;
import com.fieldchain.ex.fa100.inter.AnimationCallBack;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.Dputils;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/10
 */
public class MoneyAnimation {
    private int time = 1600;
    private ValueAnimator a1;
    private int hw, hm, h1, h2, h3, h4;

    private ImageView iv1, iv2;
    private TextView tv1, tv2;

    private int type = 2;
    private float bei = 1;

    private int c1, c2;

    public void attachView(final Activity mContext, final ImageView iv1, ImageView iv2, final TextView tv1, final TextView tv2) {
        this.iv1 = iv1;
        this.iv2 = iv2;
        this.tv1 = tv1;
        this.tv2 = tv2;

        h1 = iv1.getWidth();
        h2 = iv2.getWidth();
        if (h2 > h1) {
            int c = h1;
            h1 = h2;
            h2 = c;
        }
        hw = h1 + h2;

        h3 = Dputils.dp2px(80);
        h4 = Dputils.dp2px(36);
        bei = h3 / 80;
        initAnimation();


        c1 = Color.parseColor("#0090f7");
        c2 = Color.parseColor("#ffffff");

    }

    public void showCny() {
        if (type == 1) {
            tv1.setTextColor(c1);
            tv2.setTextColor(c2);
            iv1.setClickable(false);
            start();
            ShareManager.getInstance().saveCoinType("cny");
        }
    }

    public void showUsd() {
        if (type == 2) {
            tv1.setTextColor(c2);
            tv2.setTextColor(c1);
            iv2.setClickable(false);
            start();
            ShareManager.getInstance().saveCoinType("usd");
        }
    }

    public void initType(int type) {
        this.type = type;
    }

    private void start() {
        if (type == 2) {
            type = 1;
        } else {
            type = 2;
        }
        change();
        a1.start();
    }

    public void detachView() {
        a1.cancel();
        a1 = null;
        iv1 = null;
        iv2 = null;
        tv1 = null;
        tv2 = null;
    }

    public void initAnimation() {
//        AnticipateInterpolator开始时缓慢向前，后向后越来越快（类似弹射）
//        AccelerateInterpolator速度随着时间流逝越来越大（加速运动）
        a1 = ValueAnimator.ofInt(0, 100);
        a1.setDuration(time);
        a1.setInterpolator(new AnticipateInterpolator());
        initListener();

    }

    private void initListener() {
        MyAnimationUtils.setListener(a1, new AnimationCallBack() {
            @Override
            public void index(int pos) {
                ivShow(pos);
                marginShow(pos);
            }

            @Override
            public void onEnd() {
                iv1.setClickable(true);
                iv2.setClickable(true);
            }

        });
    }

    public void ivShow(int w) {
        ViewGroup.LayoutParams p1 = iv1.getLayoutParams();
        ViewGroup.LayoutParams p2 = iv2.getLayoutParams();
        int sw1, sw2;
        sw1 = (h1 - h2) * w / 100 + h2;
        sw2 = h1 + h2 - sw1;
        if (type == 2) {
            p1.height = sw1;
            p1.width = sw1;
            p2.height = sw2;
            p2.width = sw2;
        } else {
            p1.height = sw2;
            p1.width = sw2;
            p2.height = sw1;
            p2.width = sw1;
        }
        iv1.setLayoutParams(p1);
        iv2.setLayoutParams(p2);

    }

    public void marginShow(int t) {
        int s1, s2, m1, m2;
        s1 = 20 * t / 100 + 20;
        s2 = 60 - s1;
        m1 = 60 * t / 100 + 20;
        m2 = 100 - m1;
        if (type == 2) {
            Log.e("tags1", m1 + "");
            tv1.setPadding(0, 0, 0, (int) (m1 * bei));
            tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, s1);
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, s2);
        } else {
            Log.e("tags1", m2 + "");
            tv1.setPadding(0, 0, 0, (int) (m2 * bei));
            tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, s2);
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, s1);
        }
    }

    public void change() {
        HashMap map = new HashMap();
        map.put("type", type + "");
        HttpRequest.getInstance().postRequest(map, Constant.moneyType, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                Log.e("moneyType", code + "");
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

}
